package com.cpz.starter.config;

import com.cpz.starter.bean.MyBean;
import com.cpz.starter.config.props.BeanProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cpz
 * @since 2022/09/29
 */
@Configuration
@EnableConfigurationProperties(BeanProperties.class)
@ConditionalOnClass(MyBean.class)
@ConditionalOnProperty(prefix = "bean", name = "enable", matchIfMissing = true)
public class BeanConfig {

    @Autowired
    private BeanProperties beanProperties;

    @Bean
    @ConditionalOnMissingBean(MyBean.class)
    public MyBean myBean() {
        MyBean bean = new MyBean(beanProperties.getName());
        return bean;
    }
}
